package net.kunyv.base.controller;


import net.kunyv.base.entity.SysRole;
import net.kunyv.base.service.SysRoleService;
import net.kunyv.log.ActLog;
import net.kunyv.log.OperType;
import net.kunyv.util.PageParam;
import net.kunyv.util.PageResult;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 角色管理Controller
 *
 * @author wenq
 * @date 2018-03-13
 */
@Controller
@RequestMapping(value = "/SysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    Logger logger = LoggerFactory.getLogger(SysRoleController.class);

    /**
     * 跳转角色管理界面
     *
     * @return
     * @author sunyb
     * @date 2018-12-7
     */
    @RequiresPermissions("role:query")
    @RequestMapping(method = RequestMethod.GET, value = "/query")
    public String getRole() {
        return "login/system/sys_role";
    }

    /**
     * 跳转授权界面
     *
     * @return
     * @author sunyb
     * @date 2018-12-7
     */
    @RequiresPermissions("role:authorize")
    @RequestMapping(method = RequestMethod.GET, value = "/authorize")
    public String authorize(String roleId, ModelMap modelMap) {
        modelMap.addAttribute("roleId", roleId);
        return "login/system/authorize";
    }

    /**
     * 获取角色列表
     *
     * @return
     * @author wenq
     * @date 2018-03-13
     */
    @ResponseBody
    @RequiresPermissions("role:query")
    @ActLog(value = "查询角色", type = OperType.QUERY)
    @RequestMapping(method = RequestMethod.GET, value = "/getRole")
    public PageResult getRole(String sysCode, String roleName, PageParam pageParam) {
        return sysRoleService.getRoleList(sysCode, roleName, pageParam);
    }

    /**
     * 保存角色
     *
     * @return
     * @author wenq
     * @date 2018-03-13
     */
    @ResponseBody
    @RequiresPermissions(value = {"role:edit", "role:add"}, logical = Logical.OR)
    @ActLog(value = "保存角色", type = OperType.AddOrUpdate)
    @RequestMapping(value = "/saveRole")
    public String saveRole(SysRole sysRole) {
        return sysRoleService.saveRole(sysRole);
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return java.lang.String
     * @throw
     * @author sunyb
     * @date 2018/12/7
     */
    @ResponseBody
    @RequiresPermissions("role:delete")
    @ActLog(value = "删除角色", type = OperType.DELETE)
    @RequestMapping(value = "/deleteRoleByRoleId")
    public String deleteRoleByRoleId(String roleId) {
        return sysRoleService.deleteRoleByRoleId(roleId);
    }

    /**
     * 获取权限列表树
     *
     * @return
     * @author wenq
     * @date 2018-03-13
     */
    @ResponseBody
    @RequestMapping(value = "/selectAllPower")
    @ActLog(value = "获取权限列表树", type = OperType.QUERY)
    public List getRoleTree(HttpServletRequest request) {
        String roleId = request.getParameter("roleId");
        return sysRoleService.getRoleTree(roleId);
    }

    /**
     * 保存权限树列表
     *
     * @return
     * @author wenq
     * @date 2018-03-13
     */
    @ResponseBody
    @RequestMapping(value = "/empower")
    @ActLog(value = "保存权限树列表", type = OperType.AddOrUpdate)
    public String empower(HttpServletRequest request) {
        String roleId = request.getParameter("roleId");
        String resourceIds = request.getParameter("resourceIds");
        //存放的样子是 : [resouceId][_][datafiletertype][,]
        String datafileterIdstr = request.getParameter("datafileterIdstr");

        // HashMap params =HdUtil.getRequestParams(request);
        String[] resouceId = null;
        if (StringUtils.isNotEmpty(resourceIds)) {
            resouceId = resourceIds.split(",");
        }
        String[] datafileterIds = null;
        if (StringUtils.isNotEmpty(datafileterIdstr)) {
            datafileterIds = datafileterIdstr.split(",");
        }
        return sysRoleService.empower(roleId, resouceId, datafileterIds);
    }

}
